package com.after.saas.file.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.after.saas.client.service.RemoteDictService;
import com.after.saas.common.constants.Result;
import com.after.saas.common.exception.BaseException;
import com.after.saas.common.model.bo.CoreDictBO;
import com.after.saas.common.model.query.CommonQuery;
import com.after.saas.common.utils.EasyExcelUtil;
import com.after.saas.common.utils.ResponseUtil;
import com.after.saas.file.domain.vo.ExcelDownloadVO;
import com.after.saas.file.service.EasyExcelService;
import com.after.saas.file.utils.ExcelTemplateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 内销一般问题反馈表 前端控制器
 * </p>
 *
 * @author hjchen
 * @since 2021-03-25
 */

@Slf4j
@RestController
@Api(tags = "excel导入")
@RequestMapping("/excel")
public class EasyExcelController {

    @Autowired
    private EasyExcelService easyExcelService;

    @Autowired
    private RemoteDictService dictService;

    @ApiOperation("根据moduleCode动态导入")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public Result importExcel(@RequestParam("moduleCode") String moduleCode, @RequestParam("file") MultipartFile file) {
        log.warn("code {} dictService {}", moduleCode, dictService);
        List read;
        try {
            read = easyExcelService.read(file.getInputStream(), moduleCode);
        } catch (IOException e) {
            throw new BaseException("导入失败！" + e.getMessage());
        }
        return ResponseUtil.SUCCESS(read);
    }

    @ApiOperation("根据moduleCode下载导入模板")
    @GetMapping(value = "/download")
    public void downLoadExcel(@RequestParam(value = "moduleCode", defaultValue = "USER_IMPORT") String moduleCode, HttpServletResponse response) throws Exception {
        ExcelDownloadVO download = ExcelTemplateUtil.downloadExcel(moduleCode);
        response.reset();
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(download.getFileName(), "UTF-8"));
        try (ServletOutputStream os = response.getOutputStream(); InputStream inputStream = download.getInputStream()) {
            IoUtil.copy(inputStream, os);
        }
    }

    @GetMapping("/export")
    public Result export(HttpServletResponse response, CommonQuery query) {
        List<CoreDictBO> dictBOS = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            CoreDictBO bo = new CoreDictBO();
            bo.setDictSort(i);
            bo.setDictType("type" + (i + 1));
            bo.setDictValue("测试" + i);
            dictBOS.add(bo);
        }
        EasyExcelUtil.export(response, dictBOS, CoreDictBO.class, "测试导出");
        return ResponseUtil.SUCCESS();
    }

}
